package com.smallA.service.Impl;

import com.smallA.dao.GradeMapper;
import com.smallA.po.Grade;
import com.smallA.po.Test;
import com.smallA.service.GradeService;
import com.smallA.utils.AutoSubmit;
import com.smallA.utils.DataUtils;

import java.util.Date;
import java.util.List;

/**
 * 考试表操作实现类
 *
 * @author 君未洋
 */
public class GradeServiceImpl implements GradeService {
    private GradeMapper gradeMapper;
    private DataUtils dataUtils;

    public void setDataUtils(DataUtils dataUtils) {
        this.dataUtils = dataUtils;
    }

    public void setGradeMapper(GradeMapper gradeMapper) {
        this.gradeMapper = gradeMapper;
    }

    @Override
    public List<Grade> selectGradesById(Integer sid) {
        if (dataUtils.examineNumber(sid)) {
            return null;
        }
        return gradeMapper.selectGradesById(sid);
    }

    @Override
    public Grade selectGradeById(Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return null;
        }
        return gradeMapper.selectGradeById(gid);
    }

    @Override
    public List<Grade> selectGradeByTestId(Integer tid) {
        if (dataUtils.examineNumber(tid)) {
            return null;
        }
        return gradeMapper.selectGradeByTestId(tid);
    }

    @Override
    public boolean insertGrade(Integer sid, Integer tid) {
        //做一个小小的查询看看是否重复报名了
        if (dataUtils.examineNumber(sid, tid)) {
            return false;
        }
        if (gradeMapper.selectGradeExam(sid, tid) != null) {
            return false;
        }
        return gradeMapper.insertGrade(sid, tid) > 0;
    }


    @Override
    public boolean BeginGrade(Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return false;
        }
        Grade grade = gradeMapper.selectGradeById(gid);
        Test test = grade.getTest();
        if (test.getBeginDate().getTime() > System.currentTimeMillis()) {
            //如果设定的考试开始时间大于当前时间代表还未能开始考试,直接返回false;
            return false;
            //代表开始失败
        }
        if (test.getId() <= 0) {
            //代表这个测验有问题,终止考试
            return false;
        }
        if (grade.getId() <= 0 || grade.getStudentId() <= 0) {
            //这个是代表这个成绩表的数据有问题
            return false;
        }
        AutoSubmit autoSubmit = new AutoSubmit(test.getTestTime(), "1990609801@qq.com");
        autoSubmit.setMsg("亲爱的用户\n您参加的考试" + test.getContext() + "已结束,请在5min内提交试卷,否则将取消考试成绩");
        new Thread(autoSubmit).start();
        //开启并执行线程
        //直接开始考试
        return gradeMapper.updateGradeTime(grade.getId(), new Date()) > 0;
        //其他未判断到的情况就直接返回false
    }

    @Override
    public boolean EndGrade(Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return false;
        }
        Grade grade = gradeMapper.selectGradeById(gid);
        System.out.println(grade);
        long useTime = (System.currentTimeMillis() - grade.getBeginTime().getTime()) / (1000 * 60);
        return gradeMapper.updateGradeEnd(gid, (int) useTime) > 0;
    }


}
